package com.bush.mall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.coupon.mapper.SpuBoundsMapper;
import com.bush.mall.coupon.entity.SpuBoundsEntity;
import com.bush.mall.coupon.service.SpuBoundsService;

/**
 * 商品spu积分设置
 * @author RUN
 * @date 2022-11-29
 */
@Service(SpuBoundsService.BEAN_ID)
@Slf4j
public class SpuBoundsServiceImpl extends ServiceImpl<SpuBoundsMapper, SpuBoundsEntity> implements SpuBoundsService {

    /**
     * 查询商品spu积分设置列表
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result queryForPage(Integer current,Integer size) {
        try{
            Assert.notNull(current,"参数异常");
            Assert.notNull(size,"参数异常");
            Page<SpuBoundsEntity> page = new Page<>(current,size);
            QueryWrapper<SpuBoundsEntity> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("SpuBoundsService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建商品spu积分设置表单对象
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result saveSpuBounds(SpuBoundsEntity spuBounds) {
        try{
            Assert.notNull(spuBounds,"参数异常");
            boolean flag = this.save(spuBounds);
            if (flag) return Result.ok("商品spu积分设置添加成功");
            throw new SaaSException("商品spu积分设置添加失败");
        }catch(Exception e){
            log.error("SpuBoundsService saveSpuBounds ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改商品spu积分设置表单对象
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result updateSpuBounds(SpuBoundsEntity spuBounds) {
        try{
            Assert.notNull(spuBounds,"参数异常");
            boolean flag = this.updateById(spuBounds);
            if (flag) return Result.ok("商品spu积分设置修改成功");
            throw new SaaSException("商品spu积分设置修改失败");
        }catch(Exception e){
            log.error("SpuBoundsService updateSpuBounds ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过id删除商品spu积分设置表单
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result deleteSpuBounds(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            boolean flag = this.removeById(id);
            if (flag) return Result.ok("商品spu积分设置删除成功");
            throw new SaaSException("商品spu积分设置删除失败");
        }catch(Exception e){
            log.error("SpuBoundsService deleteSpuBounds ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }




}